﻿using System;
using System.Globalization;
using System.Windows;
using ClientBankWPF.Entities;
using ClientBankWPF.Service;
using DevExpress.Xpf.Core;

namespace ClientBankWPF
{
    /// <summary>
    /// Interaction logic for DepositAddWindow.xaml
    /// </summary>
    public partial class DepositAddWindow
    {
        private readonly DataAccessService dataAccessService;
        private readonly Generators generators;
        private readonly Guid currentUserId;
        private readonly Guid currentClientId;

        public DepositAddWindow()
        {
            InitializeComponent();

        }

        public DepositAddWindow(DataAccessService dataAccessService, Guid currentUserId, Guid currentClientId)
        {
            InitializeComponent();
            this.dataAccessService = dataAccessService;
            this.currentClientId = currentClientId;
            generators = new Generators(dataAccessService);
            this.currentUserId = currentUserId;
            textBoxDepositNumber.Text = generators.DepositNumberGenetrator().ToString();
        }

        private void ButtonCreateDepositClick(object sender, RoutedEventArgs e)
        {
            try
            {
                decimal sum = String.IsNullOrEmpty(textBoxDepositSum.Text.Trim()) ? 0
                                  : Decimal.Parse(textBoxDepositSum.Text, NumberStyles.Any, new CultureInfo("ru-RU"));
                if (sum < 0)
                {
                    DXMessageBox.Show("Введена отрицательная сумма денег", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                if (sum > Decimal.Parse("922337203685477,5807"))
                {
                    DXMessageBox.Show("Введена слишком большая сумма денег", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
                var deposit = new Deposit
                {
                    Id = Guid.NewGuid(),
                    IdClient = currentClientId,
                    Number = generators.DepositNumberGenetrator(),
                    Sum = sum,
                    CurrencyType =
                        String.IsNullOrEmpty(textBoxDepositCurrencyType.Text.Trim())
                            ? 974
                            : Int32.Parse(textBoxDepositCurrencyType.Text)
                };
                string result = dataAccessService.AddDeposit(deposit);
                if (!String.IsNullOrEmpty(result))
                {
                    DXMessageBox.Show(result, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    var depositLog = new DepositLog
                    {
                        Id = Guid.NewGuid(),
                        IdDeposit = deposit.Id,
                        IdOperationOfficer = currentUserId,
                        IsRefill = true,
                        Sum = deposit.Sum,
                        Date = DateTime.Now
                    };
                    string error = dataAccessService.AddDepositLogRecord(depositLog);
                    if (!String.IsNullOrEmpty(error))
                    {
                        DXMessageBox.Show(error, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    DXMessageBox.Show("Счёт открыт", "Информация", MessageBoxButton.OK, MessageBoxImage.Information);
                    Close();
                }
            }
            catch (Exception exception)
            {
                DXMessageBox.Show(exception.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void ButtonCancelClick(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}